package wator.view.swing;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import wator.model.Simulation;
import wator.view.swing.enums.AnimationSpeed;
import wator.view.swing.jogl.JoglDrawingStrategy;
import wator.view.swing.jogl.JoglVectorFactory2D;
import wator.view.swing.jogl.JoglVectorFactory3D;

public class SmallMenuPane extends JPanel {
	private JButton playButton;
	private JButton stopButton;
	private JButton fasterButton;
	private JButton slowerButton;
	private JButton resetButton;
	
	private boolean twoDimensional;
	private boolean gridShown;
	private SwingUI swingUI;
	private Vector<Integer> gridSize;
	
	public SmallMenuPane(SwingUI swingUI) {
		super();
		this.swingUI = swingUI;
		setPanel();
	}
	
	
	private void setPanel() {
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		setSettingsPane();
		
		setButtonPane();
		
	}
	
	private void setButtonPane() {
		JPanel buttonPane = new JPanel(new GridBagLayout());
		buttonPane.setBorder(BorderFactory.createTitledBorder("Play settings"));
				
		String path = "icon" + System.getProperty("file.separator");
		System.out.println(path);
		
		stopButton = new JButton();
		stopButton.setIcon(new ImageIcon(path + "stop_small.png"));
		stopButton.setToolTipText("Stop/Pause the Animation");
		stopButton.setEnabled(false);
		stopButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				swingUI.setAnimationSpeed(AnimationSpeed.STOP);
				stopButton.setEnabled(false);
				playButton.setEnabled(true);
				fasterButton.setEnabled(true);
				slowerButton.setEnabled(true);
				swingUI.simulate();
			}
		});
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.gridx = 0;
		gbc.gridy = 0;
		buttonPane.add(stopButton, gbc);

		slowerButton = new JButton();
		slowerButton.setIcon(new ImageIcon(path + "play_small.png"));
		slowerButton.setToolTipText("Normal Animation Speed");
		slowerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				swingUI.setAnimationSpeed(AnimationSpeed.SLOW);
				stopButton.setEnabled(true);
				playButton.setEnabled(true);
				fasterButton.setEnabled(true);
				slowerButton.setEnabled(false);
				swingUI.simulate();
			}
		});
		gbc.gridx = 1;
		buttonPane.add(slowerButton, gbc);
		
		playButton = new JButton();
		playButton.setIcon(new ImageIcon(path + "faster_small.png"));
		playButton.setToolTipText("Faster Animation Speed");
		playButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				swingUI.setAnimationSpeed(AnimationSpeed.NORMAL);
				stopButton.setEnabled(true);
				playButton.setEnabled(false);
				fasterButton.setEnabled(true);
				slowerButton.setEnabled(true);
				swingUI.simulate();
			}
		});
		gbc.gridx = 2;
		buttonPane.add(playButton, gbc);
		
		fasterButton = new JButton();
		fasterButton.setIcon(new ImageIcon(path + "fastest_small.png"));
		fasterButton.setToolTipText("The Fastest Animation Speed");
		//fasterButton.setBackground(Color.TRANSLUCENT);
		fasterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				swingUI.setAnimationSpeed(AnimationSpeed.FAST);
				stopButton.setEnabled(true);
				playButton.setEnabled(true);
				fasterButton.setEnabled(false);
				slowerButton.setEnabled(true);
				swingUI.simulate();
			}
		});
		gbc.gridx = 3;
		buttonPane.add(fasterButton, gbc);
		
		resetButton = new JButton("Reset");
		resetButton.setToolTipText("Reset Animation - Start again");
		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Simulation.getInstance().getPopulation().repopulate();
			}
		});
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.BOTH;
		buttonPane.add(resetButton, gbc);
		
		this.add(buttonPane);    
	}
	
	private void setSettingsPane() {
		JPanel settings = new JPanel(new GridBagLayout());
		settings.setBorder(BorderFactory.createTitledBorder("Settings"));
	    int maxGridSize = SwingUI.MAX_GRID_SIZE;
		SpinnerNumberModel snmZ = new SpinnerNumberModel(10, 3, maxGridSize, 1);
		final JSpinner zGrid= new JSpinner(snmZ);;
		 
		// Dimension settings =================================================
		JLabel dimLabel = new JLabel("Dimension: ");
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0; c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(5, 5, 5, 5);
		settings.add(dimLabel, c);
		
		ButtonGroup group = new ButtonGroup();
	    final JRadioButton twoD = new JRadioButton("2D", true);
	    group.add(twoD);
	    c.gridx = 1;
	    settings.add(twoD, c);
	    twoD.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				swingUI.getGrid().setTwoDimensional(twoD.isSelected());
				JoglDrawingStrategy.setJoglVectorFactory(
						new JoglVectorFactory2D());
				zGrid.setEnabled(false);
			}
		});
	    
	    final JRadioButton threeD = new JRadioButton("3D");
	    group.add(threeD);
	    c.gridx = 2;
	    settings.add(threeD, c);
	    threeD.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				swingUI.getGrid().setTwoDimensional(twoD.isSelected());
				JoglDrawingStrategy.setJoglVectorFactory(
						new JoglVectorFactory3D());
				zGrid.setEnabled(true);
			}
		});
	    threeD.setSelected(true);
	    // Dimension settings ==============================================end
	    
	    // grid settings ======================================================
	    final JCheckBox gridBox = new JCheckBox("Show neighborhood radius");
	    gridBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//swingUI.getGrid().setVisible(gridBox.isSelected());
				// TODO
				JoglDrawingStrategy.setShowNeighborhoodRadius(
						!JoglDrawingStrategy.isShowNeighborhoodRadius());
			}
		});
	    c = new GridBagConstraints();
	    c.gridwidth = 3;
	    c.anchor = GridBagConstraints.WEST;
	    c.gridx = 3;
	    c.insets = new Insets(5, 5, 5, 5);
	    settings.add(gridBox, c);
	    
	    
	    JLabel gridLabel = new JLabel("Choose the size of the grid (x, y, z)");
	    c.gridx = 0; c.gridy = 1;
	    c.gridwidth = 3;
	    c.fill = GridBagConstraints.HORIZONTAL;
	    settings.add(gridLabel, c);
	    
	    gridSize = new Vector<Integer>();
	    SpinnerNumberModel snmX = new SpinnerNumberModel(10, 3, maxGridSize, 1);
	    final JSpinner xGrid = new JSpinner(snmX);
	    c = new GridBagConstraints();
	    c.gridx = 4; c.gridy = 1;
	    c.insets = new Insets(5, 0, 5, 5);
	    settings.add(xGrid, c);
	    xGrid.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				swingUI.getGrid().updateSizeX(
						Integer.parseInt(xGrid.getValue().toString()));
			}
		});
	    
	    SpinnerNumberModel snmY = new SpinnerNumberModel(10, 3, maxGridSize, 1);
	    final JSpinner yGrid = new JSpinner(snmY);
	    c.gridx = 5;
	    c.insets = new Insets(5, 5, 5, 5);
	    settings.add(yGrid, c);
	    yGrid.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				swingUI.getGrid().updateSizeY(
						Integer.parseInt(yGrid.getValue().toString()));
			}
		});
	    
	    
	    c.gridx = 6;
	    settings.add(zGrid, c);
	    zGrid.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				swingUI.getGrid().updateSizeZ(
						Integer.parseInt(zGrid.getValue().toString()));
			}
		});
	    zGrid.setEnabled(threeD.isSelected());
	    
	    // grid settings ===================================================end
	       
	    this.add(settings);
		
	}
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected ImageIcon createImageIcon(String path, String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
	
}
